<?php
namespace App\Http\Controllers\Admin;


use App\Models\Admin\DataProduct;
use App\Models\Factory\DataProductCountFactory;
use App\Models\Orm\DataProductDetailLog;
use App\Models\Orm\DataProductApplyLog;
use App\Models\Orm\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Orm\Productdata;
/**
 *产品表现数据
 * Class ProductDataController
 * @package App\Http\Controllers\Controller;
 */
class DataProductController extends Controller
{

    // 导出昨天产品总览
    const EXCEL_YESTERDAY = 0;
    // 导出30天产品总览
    const EXCEL_MONTH = 1;
    // 导出某产品所有日志
    const EXCEL_PRODUCT = 2;

    // 列表页表头
    private $list_excel_header = [
        '产品名称',
        '所属平台',
        '昨天PV',
        '昨天UV',
        '昨天按钮点击次数',
        '昨天按钮点击人数',
    ];

    // 日志页表头
    private $log_excel_header = [
        '记录日期',
        'PV',
        'UV',
        '按钮点击次数',
        '按钮点击人数',
    ];




    /**
     *产品表现数据（展示页）
     * @return \Illuminate\Http\Response
     *
     */
    public function index(Request $request){
        //实例化model
//        $model = new DataProduct();

        $date = $request->input('date', date('Y-m-d'));

        $tab = $request->input('tab', 0);

        if (!$tab) {
            if ($date == date('Y-m-d')) {
                // 今天数据
                $data_list = [];

                // 详情页点击日志记录数据
                $dayDetailLogs = $this->getDayDetailLogs($date);

                if (!empty($dayDetailLogs)) {

                    foreach ($dayDetailLogs as $dayDetailLog) {

                        $key = $dayDetailLog['product_id'];

                        if (!isset($data_list[$key])) {

                            // 初始化这个产品统计记录
                            $data_list[$key] = [
                                'detail_ip_list' => [$dayDetailLog['create_ip']],
                                'apply_id_list' => [],
                                'product_id' => $dayDetailLog['product_id'],
                                'product_name' => $dayDetailLog['product_name'],
                                'details_total' => 1,
                                'details_user_total' => 1,
                                'apply_total' => 0,
                                'user_total' => 0,
                                'product_logo' => $dayDetailLog['product_logo'],
                                'create_ip_list' => [],

                            ];

                        } else {

                            // 更新这个产品统计记录
                            $data_list[$key]['details_total'] += 1;

                            if (!in_array($dayDetailLog['create_ip'], $data_list[$key]['detail_ip_list'])) {

                                $data_list[$key]['details_user_total'] += 1;

                                $data_list[$key]['detail_ip_list'][] = $dayDetailLog['create_ip'];

                            }
                        }
                    }
                }

                // 按钮点击日志记录数据
                $dayApplyLogs = $this->getDayApplyLogs($date);

                if (!empty($dayApplyLogs)) {

                    foreach ($dayApplyLogs as $dayApplyLog) {

                        $key = $dayApplyLog['product_id'];

                        if (!isset($data_list[$key])) {

                            // 初始化这个产品统计记录
                            $data_list[$key] = [
                                'apply_id_list' => [$dayApplyLog['user_id']],
                                'detail_ip_list' => [],
                                'product_id' => $dayApplyLog['product_id'],
                                'product_name' => $dayApplyLog['product_name'],
                                'details_total' => 0,
                                'details_user_total' => 0,
                                'apply_total' => 1,
                                'user_total' => 1,
                                'product_logo' => $dayApplyLog['product_logo'],
                                'create_ip_list' => [],
                            ];

                        } else {

                            // 更新这个产品统计记录
                            $data_list[$key]['apply_total'] += 1;

                            if (!in_array($dayApplyLog['create_ip'], $data_list[$key]['create_ip_list'])) {

                                $data_list[$key]['user_total'] += 1;

                                $data_list[$key]['create_ip_list'][] = $dayApplyLog['create_ip'];

                            }
                        }
                    }
                }

                foreach ($data_list as $key => $item) {
                    unset($data_list[$key]['apply_id_list']);
                    unset($data_list[$key]['detail_ip_list']);
                }

            } else {
                // 某天数据
                $data_list = DataProductCountFactory::getDayDataProductCount($date)->toArray();
            }

//            dd($data_list);
            return view('admin.productdata.yesterday', compact('data_list', 'date'));
        }


        // 否则显示30天数据
        $data_list = DataProductCountFactory::getMonthDataProductCount();
        return view('admin.productdata.month', compact('active', 'atc', 'data_list'));
    }

    /**
     * 某一天详情页数据统计
     */
    private function getDayDetailLogs($date)
    {
        $start = $date . ' 00:00:00';
        $end = $date . ' 24:00:00';

        $res = DataProductDetailLog::select([
            'product_id',
            'product_name',
            'create_ip',
            'user_id',
            'product.product_logo',
        ])
            ->leftjoin('product', 'product.id', '=', 'data_product_detail_log.product_id')
            ->where('create_at', '>', $start)
            ->where('create_at', '<=', $end)
            ->get()
            ->toArray();
        return $res ?: [];

    }

    /**
     * 某一天申请按钮数据统计
     */
    private function getDayApplyLogs($date)
    {
        $start = $date . ' 00:00:00';
        $end = $date . ' 24:00:00';
        $res = DataProductApplyLog::select([
            'product_id',
            'product_name',
            'user_id',
            'product.product_logo',
            'create_ip'
        ])
            ->leftjoin('product', 'product.id', '=', 'data_product_apply_log.product_id')
            ->where('create_at', '>', $start)
            ->where('create_at', '<=', $end)
            ->get()
            ->toArray();
        return $res ?: [];

    }
    public function list($product_id)
    {


        $datas =DataProductCountFactory::getProductCountList($product_id);
        $product = Product::where('id',$product_id)->first();
        $product_name  = $product->product_name;

//        dd($product_id);
        return view('admin.productdata.list', compact('product_name','product_id', 'datas'));
    }


}